home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hscprj / writeprj.c < prev   
Encoding:
C/C++ Source or Header  |  1997-11-02  |  8.9 KB  |  350 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * hscprj/writeprj.c
  22.  *
  23.  * project-managment output-routines for hsc
  24.  *
  25.  * updated: 28-Mar-1997
  26.  * created: 10-Sep-1996
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <time.h>
  34.  
  35. #include "hsclib/ldebug.h"
  36. #include "hscprj/pdebug.h"
  37. #include "hscprj/pdefs.h"
  38.  
  39. #include "ugly/utypes.h"
  40. #include "ugly/dllist.h"
  41. #include "ugly/expstr.h"
  42. #include "ugly/umemory.h"
  43. #include "ugly/infile.h"
  44. #include "ugly/ustring.h"
  45.  
  46. #include "hscprj/document.h"
  47. #include "hscprj/project.h"
  48.  
  49. #define TIMEBUFSIZE 40
  50. static STRARR timebuf[TIMEBUFSIZE];
  51.  
  52. /*
  53.  *-------------------------------------
  54.  * write project file
  55.  *-------------------------------------
  56.  */
  57.  
  58. /*
  59.  * hsc_project_write_data
  60.  *
  61.  * write ids to file
  62.  */
  63. static VOID append_ulong(EXPSTR * dest, ULONG num)
  64. {
  65.     STRARR lenbuf[20];
  66.  
  67.     sprintf(lenbuf, "%lx ", num);
  68.     app_estr(dest, lenbuf);
  69. }
  70.  
  71. static VOID append_string(EXPSTR * dest, STRPTR s)
  72. {
  73.     append_ulong(dest, (ULONG) strlen(s));
  74.     app_estr(dest, s);
  75. }
  76.  
  77. static VOID append_caller(EXPSTR * dest, CALLER * caller)
  78. {
  79. #if 0
  80.     if (caller)
  81.     {
  82.         app_estr(dest, " " ID_CALLER_STR " ");
  83.         append_string(dest, caller->name);
  84.         append_ulong(dest, caller->posx);
  85.         append_ulong(dest, caller->posy);
  86.     }
  87. #endif
  88. }
  89.  
  90. static VOID append_docname(EXPSTR * prjstr, STRPTR docname)
  91. {
  92.     app_estr(prjstr, LINE_DOCUMENT_STR);
  93.     app_estr(prjstr, " ");
  94.     append_string(prjstr, docname);
  95.     app_estr(prjstr, "\n");
  96. }
  97.  
  98. static VOID append_sourcename(EXPSTR * prjstr, STRPTR sourcename)
  99. {
  100.     if (sourcename)
  101.     {
  102.         app_estr(prjstr, LINE_SOURCE_STR);
  103.         app_estr(prjstr, " ");
  104.         append_string(prjstr, sourcename);
  105.         app_estr(prjstr, "\n");
  106.     }
  107. }
  108.  
  109. static VOID append_title(EXPSTR * prjstr, STRPTR title)
  110. {
  111.     if (title)
  112.     {
  113.         app_estr(prjstr, LINE_TITLE_STR);
  114.         app_estr(prjstr, " ");
  115.         append_string(prjstr, title);
  116.         app_estr(prjstr, "\n");
  117.     }
  118. }
  119.  
  120. static VOID append_id(EXPSTR * prjstr, STRPTR id)
  121. {
  122.     app_estr(prjstr, LINE_ID_STR);
  123.     app_estr(prjstr, " ");
  124.     append_string(prjstr, id);
  125.     app_estr(prjstr, "\n");
  126. }
  127.  
  128. static VOID append_include(EXPSTR * prjstr, HSCINC * include)
  129. {
  130.     app_estr(prjstr, LINE_INCLUDE_STR);
  131.     app_estr(prjstr, " ");
  132.     append_string(prjstr, include->name);
  133.     append_caller(prjstr, include->caller);
  134.     app_estr(prjstr, "\n");
  135. #if 0
  136.     DP(fprintf(stderr, DHP "  include `%s'\n", include->name));
  137. #endif
  138. }
  139.  
  140. static VOID append_reference(EXPSTR * prjstr, HSCREF * reference)
  141. {
  142. #if 1
  143.     app_estr(prjstr, LINE_REFERENCE_STR);
  144.     app_estr(prjstr, " ");
  145.     append_string(prjstr, reference->name);
  146.     append_caller(prjstr, reference->caller);
  147.     app_estr(prjstr, "\n");
  148. #else
  149.     DP(fprintf(stderr, DHP "  refers `%s'\n", reference->name));
  150. #endif
  151. }
  152.  
  153. static VOID append_iddef(EXPSTR * prjstr, HSCIDD * iddef)
  154. {
  155.     app_estr(prjstr, LINE_ID_STR);
  156.     app_estr(prjstr, " ");
  157.     append_string(prjstr, iddef->name);
  158.     append_caller(prjstr, iddef->caller);
  159.     app_estr(prjstr, "\n");
  160. }
  161.  
  162. /*
  163.  * append groups of data
  164.  */
  165. static VOID append_header(EXPSTR * prjstr)
  166. {
  167.     time_t now = time(NULL);    /* get current time */
  168.  
  169.     /* create string for current time */
  170.     strftime(timebuf, TIMEBUFSIZE,
  171.              "%d-%b-%Y %H:%M:%S", localtime(&now));
  172.  
  173.     /* append key-sequence, file-format-version and comment */
  174.     app_estr(prjstr,
  175.              FILEID_HSCPRJ "\n" LINE_VERSION_STR " ");
  176.     append_ulong(prjstr, VERSION_HSCPRJ);
  177.     app_estr(prjstr, "\n");
  178.     app_estr(prjstr,
  179.              LINE_REM_STR " Contains all data relevant for project.\n"
  180.              LINE_REM_STR " Maintained by hsc, DO NOT MODIFY!\n");
  181.     app_estr(prjstr,
  182.              LINE_REM_STR " updated: ");
  183.     app_estr(prjstr, timebuf);
  184.     app_estrch(prjstr, '\n');
  185. }
  186.  
  187. /* append included files */
  188. static VOID append_doc_includes(EXPSTR * prjstr, DLLIST * inclist)
  189. {
  190.     DLNODE *nd = dll_first(inclist);
  191.     while (nd)
  192.     {
  193.         append_include(prjstr, (HSCINC *) dln_data(nd));
  194.         nd = dln_next(nd);
  195.     }
  196. }
  197.  
  198. /* append other documents referered */
  199. static VOID append_doc_references(EXPSTR * prjstr, DLLIST * reflist)
  200. {
  201.     DLNODE *nd = dll_first(reflist);
  202.     while (nd)
  203.     {
  204.         append_reference(prjstr, (HSCREF *) dln_data(nd));
  205.         nd = dln_next(nd);
  206.     }
  207. }
  208.  
  209. /* append ids defined within documents */
  210. static VOID append_doc_iddefs(EXPSTR * prjstr, DLLIST * iddefs)
  211. {
  212.     DLNODE *nd = dll_first(iddefs);
  213.     while (nd)
  214.     {
  215.         append_iddef(prjstr, (HSCIDD *) dln_data(nd));
  216.         nd = dln_next(nd);
  217.     }
  218. }
  219.  
  220. /* append all document-related data */
  221. static VOID append_document(EXPSTR * prjstr, HSCDOC * document)
  222. {
  223.     STRPTR docname = document->docname;
  224.  
  225.     append_docname(prjstr, docname);
  226.     append_sourcename(prjstr, document->sourcename);
  227.     append_title(prjstr, estr2str(document->title));
  228.  
  229.     append_doc_iddefs(prjstr, document->iddefs);
  230.     append_doc_includes(prjstr, document->includes);
  231. #if 0
  232.     append_doc_references(prjstr, document->references);
  233. #endif
  234. }
  235.  
  236. /****** hscprj.lib/hsc_project_write_data ************************************
  237. *
  238. *   NAME
  239. *       hsc_project_write_data -- store project data in a file
  240. *
  241. *   SYNOPSIS
  242. *       ok = hsc_project_write_data( project, filename, force )
  243. *
  244. *       BOOL hsc_project_write_data
  245. *            ( HSCPRJ * project, STRPTR filename, BOOL force )
  246. *
  247. *   FUNCTION
  248. *       Store all project data in a file. If the file already exists, it
  249. *       will be overwritten without asking back.
  250. *
  251. *   INPUTS
  252. *       project  - project data
  253. *       filename - filename of project file
  254. *       force    - write data even if no new documents have been atteched;
  255. *                  enable this, if e.g. you only have removed some documents
  256. *
  257. *   RESULT
  258. *       ok - TRUE, if file could have been written successfully
  259. *
  260. *   EXAMPLE
  261. *       ok = hsc_project_write_data( project, "sepp.project", FALSE );
  262. *
  263. *   SEE ALSO
  264. *
  265. *
  266. ******************************************************************************
  267. */
  268. BOOL hsc_project_write_data(HSCPRJ * hp, STRPTR project_fname, BOOL force)
  269. {
  270.     BOOL written = FALSE;
  271.  
  272.     if (hp && !hp->fatal && project_fname)
  273.     {
  274.         EXPSTR *prjstr = init_estr(256);
  275.         DLNODE *nd = NULL;
  276.         FILE *outfile = NULL;
  277.         BOOL write_it = force; /* really write it? */
  278.  
  279.         DP(fprintf(stderr, DHP "update project file `%s'\n",
  280.                    project_fname));
  281.  
  282.         /* append header information */
  283.         append_header(prjstr);
  284.  
  285.         if ((hp->document) && (hp->document->docname))
  286.         {
  287.             /* append current document to project */
  288.             hsc_project_add_document(hp);
  289.  
  290.             /* now it needs an update.. */
  291.             write_it = TRUE;
  292.         }
  293.  
  294.         if (write_it)
  295.         {
  296.             /*
  297.              * append all old project info of other files
  298.              */
  299.             nd = dll_first(hp->documents);
  300.             while (nd)
  301.             {
  302.                 HSCDOC *document = (HSCDOC *) nd->data;
  303.  
  304.                 append_document(prjstr, document);
  305.  
  306.                 nd = dln_next(nd);
  307.             }
  308.  
  309.             DP(fprintf(stderr, DHP "project file contains:\n%s", estr2str(prjstr)));
  310.  
  311.             /* write new project file */
  312.             errno = 0;
  313.             outfile = fopen(project_fname, "w");
  314.             if (outfile)
  315.             {
  316.                 errno = 0;
  317.                 fwrite(estr2str(prjstr), sizeof(char),
  318.                        estrlen(prjstr), outfile);
  319.  
  320.                 if (errno)
  321.                 {
  322.                     DP(fprintf(stderr, DHP "can't write project file\n"));
  323.                     /* TODO: show message "can't write project file" */
  324.                 }
  325.                 else
  326.                     written = TRUE;
  327.             }
  328.             else
  329.             {
  330.                 DP(fprintf(stderr, DHP "can't open project file for output\n"));
  331.                 /* TODO: show message "can't open project file" */
  332.             }
  333.         }
  334.         else
  335.         {
  336.             DP(fprintf(stderr, DHP "  no document or docname to add\n"));
  337.             del_document(hp->document);
  338.             hp->document = NULL;
  339.         }
  340.  
  341.         del_estr(prjstr);
  342.     }
  343.     else
  344.     {
  345.         D(fprintf(stderr, DHP "no update project\n"));
  346.     }
  347.  
  348.     return (written);
  349. }
  350.